package com.pirestupppai.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.pirestupppai.R;
import com.pirestupppai.adapter.FollowListAdapter;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.base.BaseAdapter;
import com.pirestupppai.base.BaseStatus;
import com.pirestupppai.bean.TupaiUser;
import com.pirestupppai.common.Constant;
import com.pirestupppai.network.NetworkRequest;
import com.pirestupppai.network.TupaiSubscriber;
import com.pirestupppai.widgets.view.CustomRecyclerView;
import com.pirestupppai.widgets.view.LoadMoreFooter;
import com.pirestupppai.widgets.view.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;


/**
 * 关注粉丝列表
 *
 * @author: xiaoluo
 * @date: 2017-04-14 11:14
 */
public class FollowListActivity extends BaseActivity {

    public static int FOLLOW_TYPE_MESSAGE = 0;   // 聊天界面 关注的好友
    public static int FOLLOW_TYPE_FOLLOW = 1;    // 我的关注
    public static int FOLLOW_TYPE_FANS = 2;      // 我的粉丝

    @BindView(R.id.follow_recycler_view)
    CustomRecyclerView recyclerView;
    @BindView(R.id.follow_list_titlebar)
    TitleBar titleBar;

    private LoadMoreFooter mFooterView;
    private FollowListAdapter mFollowAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    private int mUid = -1;
    private int mPosition = 0;  // 列表最后一位
    private int mPage = 1;
    private int mSize = 10;
    private List<TupaiUser> mUserList = new ArrayList<>();
    private int mFollowType = 0;
    private boolean hasMore = true;

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_follow_list;
    }

    @Override
    protected void initViews() {
        mFollowType = getIntent().getIntExtra(Constant.IntentKey.FOLLOW_LIST_TYPE, 1);
        mUid = getIntent().getIntExtra(Constant.IntentKey.UID, -1);

        if (mFollowType == FOLLOW_TYPE_FANS) {
            titleBar.setTitle("粉丝");
            recyclerView.setEmptyText("暂时没有任何粉丝哟");
        } else if (mFollowType == FOLLOW_TYPE_MESSAGE) {
            titleBar.setTitle("关注的好友");
            recyclerView.setEmptyText("暂时没有任何关注哟");
        } else {
            titleBar.setTitle("关注");
            recyclerView.setEmptyText("暂时没有任何关注哟");
        }

        mFooterView = new LoadMoreFooter(mContext);
        mFollowAdapter = new FollowListAdapter(mContext, mUserList, mFollowType);
        mFollowAdapter.addFooterView(mFooterView);
        mLinearLayoutManager = new LinearLayoutManager(mContext);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.getRecyclerView().setLayoutManager(mLinearLayoutManager);
        recyclerView.getRecyclerView().setAdapter(mFollowAdapter);
        recyclerView.getRecyclerView().setHasFixedSize(true);
    }

    @Override
    protected void initListeners() {
        // 监听到达底部事件
        recyclerView.getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //到达底部时,加载更多
                int lastVisiblePosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition() + 1;
                if (mLinearLayoutManager.getChildCount() > 0
                        && mLinearLayoutManager.getItemCount() >= mLinearLayoutManager.getChildCount()
                        && lastVisiblePosition >= mLinearLayoutManager.getItemCount()
                        && dy > 0) {
                    mPosition = lastVisiblePosition;

                    if (hasMore) {
                        mFooterView.setStatus(LoadMoreFooter.Status.LOADING);
                        getData(BaseStatus.LOADMORE);
                    }
                }
            }
        });

        mFollowAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, final int position) {
                if (mFollowType == FOLLOW_TYPE_MESSAGE) {
                    final String id = Integer.toString(mUserList.get(position).getId());
                    final String nickname = mUserList.get(position).getNickname();
                    final Uri avatar = Uri.parse(mUserList.get(position).getAvatar() + "?imageView2/2/w/200/h/200");
                    RongIM.setUserInfoProvider(new RongIM.UserInfoProvider() {
                        @Override
                        public UserInfo getUserInfo(String s) {
                            return new UserInfo(id, nickname, avatar);
                        }
                    }, true);
                    RongIM.getInstance().refreshUserInfoCache(new UserInfo(id, nickname, avatar));
                    RongIM.getInstance().startPrivateChat(mContext, id, nickname);
                } else {
                    Intent intent = new Intent(mContext, UserActivity.class);
                    intent.putExtra(Constant.IntentKey.UID, mUserList.get(position).getId());
                    mContext.startActivity(intent);
                }
            }
        });

    }

    @Override
    protected void initLogic() {
        getData(BaseStatus.REFRESH);
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 获取数据
     */
    private void getData(final BaseStatus status) {
        if (status == BaseStatus.REFRESH) {
            mFooterView.setStatus(LoadMoreFooter.Status.GONE);
            hasMore = true;
            mPage = 1;
        } else {
            mPage++;
        }

        NetworkRequest.getInstance().getFollowList(mUid, mPage, mSize, mFollowType, this,
                new TupaiSubscriber<List<TupaiUser>>(mContext) {
                    @Override
                    protected void requestSuccess(List<TupaiUser> users) {
                        if (users != null && users.size() != 0) {
                            if (status == BaseStatus.REFRESH) {
                                mUserList.clear();
                                mUserList.addAll(users);
                                mFollowAdapter.notifyDataSetChanged();
                            } else {
                                mUserList.addAll(users);
                                mFollowAdapter.notifyItemInserted(mPosition);
                            }

                        } else if (status == BaseStatus.REFRESH) {
                            recyclerView.setEmpty();
                        } else {
                            mFooterView.setStatus(LoadMoreFooter.Status.THE_END);
                        }

                        if (users != null && users.size() < mSize) {
                            mFooterView.setStatus(LoadMoreFooter.Status.THE_END);
                            hasMore = false;
                        }
                    }

                    @Override
                    protected void requestError(String error) {
                        if (status == BaseStatus.LOADMORE) {
                            mFooterView.setStatus(LoadMoreFooter.Status.GONE);
                            mPage--;
                        }
                    }
                });
    }
}
